package com.news.activity;

import java.util.List;

import com.xiang.news.R;
import com.news.contant.Contant;
import com.news.view.TittleListViewPagerAdapter;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

/**
 * 
 * 带viewpager 的首页
 */
public class MainViewPagerActivity extends Activity implements OnClickListener,
		OnPageChangeListener {
	private static final String TAG = "ScrollMenu---->";
	/** 各菜单TextView控件 */
	private TextView txv_new_center;
	private TextView txv_new_inland;
	private TextView txv_new_internation;
	private TextView txv_new_society;
	private TextView txv_new_war;
	private TextView txv_new_sport;
	private TextView txv_new_it;
	private TextView txv_new_digital;
	private TextView txv_new_phone;
	private TextView txv_new_computer;
	private TextView txv_new_fun;
	private TextView txv_new_hot;
	private TextView txv_new_travel;
	private TextView txv_new_star;
	private TextView txv_new_china_football;
	private TextView txv_new_national_football;
	private TextView txv_new_nba;
	private TextView txv_new_cba;
	
	private TextView curTxt;
	
	private ImageView more;
	
	/** 顶部横向ScrollView */
	private HorizontalScrollView horiScroll;
	private ViewPager vPager_Sc;
	/** ViewPager内容数据列表 */
//	private List<View> pageList;
	private List<String> pageList;
	/** 移动小方框 */
	private ImageView imgTransBg;
	/** 自定义ViewPager适配器 */
	private TittleListViewPagerAdapter mAdapter;
	/** 手机屏幕的宽度 */
	private int disWidth;
	/** 调整ScrollView位置时计算的偏差 */
	private int offset = 0;
	/** 是否已经计算了偏差的标识 */
	private boolean hasOffset = false;
	
	private long exitTime = 0;// 再按一次返回键退出程序使用

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_viewpager_layout);
		initView();
		setPageAdapter();
	}

	private void initView() {
		horiScroll = (HorizontalScrollView) findViewById(R.id.horiScroll);
		vPager_Sc = (ViewPager) findViewById(R.id.vPager_Sc);
		imgTransBg = (ImageView) findViewById(R.id.imgTransBg);
		txv_new_center = (TextView) findViewById(R.id.txv_new_center);
		txv_new_inland = (TextView) findViewById(R.id.txv_new_inland);
		txv_new_internation = (TextView) findViewById(R.id.txv_new_internation);
		txv_new_society = (TextView) findViewById(R.id.txv_new_society);
		txv_new_war = (TextView) findViewById(R.id.txv_new_war);
		txv_new_sport = (TextView) findViewById(R.id.txv_new_sport);
		txv_new_it = (TextView) findViewById(R.id.txv_new_it);
		txv_new_digital = (TextView) findViewById(R.id.txv_new_digital);
		txv_new_phone = (TextView) findViewById(R.id.txv_new_phone);
		txv_new_computer = (TextView) findViewById(R.id.txv_new_computer);
		txv_new_fun = (TextView) findViewById(R.id.txv_new_fun);
		txv_new_hot = (TextView) findViewById(R.id.txv_new_hot);
		txv_new_travel = (TextView) findViewById(R.id.txv_new_travel);
		txv_new_star = (TextView) findViewById(R.id.txv_new_star);
		txv_new_china_football = (TextView) findViewById(R.id.txv_new_china_football);
		txv_new_national_football = (TextView) findViewById(R.id.txv_new_national_football);
		txv_new_nba = (TextView) findViewById(R.id.txv_new_nba);
		txv_new_cba = (TextView) findViewById(R.id.txv_new_cba);
		
		more = (ImageView) findViewById(R.id.img_main_more);
		
		
		txv_new_center.setOnClickListener(this);
		txv_new_inland.setOnClickListener(this);
		txv_new_internation.setOnClickListener(this);
		txv_new_society.setOnClickListener(this);
		txv_new_war.setOnClickListener(this);
		txv_new_sport.setOnClickListener(this);
		txv_new_it.setOnClickListener(this);
		txv_new_digital.setOnClickListener(this);
		txv_new_phone.setOnClickListener(this);
		txv_new_computer.setOnClickListener(this);
		txv_new_fun.setOnClickListener(this);
		txv_new_hot.setOnClickListener(this);
		txv_new_travel.setOnClickListener(this);
		txv_new_star.setOnClickListener(this);
		txv_new_china_football.setOnClickListener(this);
		txv_new_national_football.setOnClickListener(this);
		txv_new_nba.setOnClickListener(this);
		txv_new_cba.setOnClickListener(this);
		vPager_Sc.setOnPageChangeListener(this);
		
		more.setOnClickListener(this);
		// 当前TextView默认为第一个
		curTxt = txv_new_center;
		// 设置默认位置的字体颜色为白色的选中效果
		txv_new_center.setTextColor(getResources().getColor(R.color.white));
		// 获取手机屏幕宽度
		disWidth = getWindowManager().getDefaultDisplay().getWidth();
		Log.i(TAG, "手机屏幕宽度disWidth:" + disWidth);
	}

	/** 设置ViewPager数据适配器 */
	private void setPageAdapter() {
		pageList = Contant.ulrList();
		TittleListViewPagerAdapter mAdapter = new TittleListViewPagerAdapter( this, Contant.ulrList());
		vPager_Sc.setAdapter(mAdapter);
	}

	/**
	 * 设置小方框的移动动画效果(没计算偏差,但基本效果能完全实现)
	 * 
	 * @param endTxt
	 */
	private void imgTrans(TextView endTxt) {
		// 当前TextView的中心点
		int startMid = curTxt.getLeft() + curTxt.getWidth() / 2;
		// 移动开始位置左边缘
		int startLeft = startMid - imgTransBg.getWidth() / 2;
		// 目的TextView的中心点
		int endMid = endTxt.getLeft() + endTxt.getWidth() / 2;
		// 移动结束位置左边缘
		int endLeft = endMid - imgTransBg.getWidth() / 2;
		// 构造动画
		TranslateAnimation move = new TranslateAnimation(startLeft, endLeft, 0,
				0);
		move.setDuration(100);
		move.setFillAfter(true);
		imgTransBg.startAnimation(move);

		/*
		 * 以下步骤用于处理ScrollView根据滑块的位置来调整自身的滚动, 以便达到更好的视觉效果
		 */
		int[] location = new int[2];
		// 获取目的TextViw在当前屏幕中的坐标点,主要用到X轴方向坐标:location[0]
		endTxt.getLocationOnScreen(location);
		// 调整ScrollView的位置
		if (location[0] < 0) {
			// 目的位置超出左边屏幕,则调整到紧靠该位置的左边
			// 此处ScrollView直接根据位置点滑动
			horiScroll.smoothScrollTo(endTxt.getLeft(), 0);
		} else if ((location[0] + endTxt.getWidth()) > disWidth) {
			// 目的位置超出右边屏幕,则调整到紧靠该位置的右边
			// 此处ScrollView需计算滑动距离
			horiScroll.smoothScrollBy(location[0] + endTxt.getWidth()
					- disWidth, 0);
		} else {
			// 此处如果没超出屏幕,也需保持原地滑动
			// 如果不加该效果,则滑块可能出现动画延迟或停滞
			horiScroll.smoothScrollTo(horiScroll.getScrollX(), 0);
		}

		// 切换字体颜色
		curTxt.setTextColor(getResources().getColor(R.color.gray));
		endTxt.setTextColor(getResources().getColor(R.color.white));

		// 更新当前TextView的记录
		curTxt = endTxt;
	}

	/** 偏差计算的时候用到,不用则可以忽略 */
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			/*
			 * 计算getLocationOnScreen()可能带来的偏差:
			 * 因为getLocationOnScreen()方法要等UI绘制完才能获取正确的值, 所以不能在onCreate()中直接计算,
			 * 此处设置一个boolean标识,在第一次触动屏幕时获取一次
			 */
			if (curTxt == txv_new_center && !hasOffset) {
				// 获取第一个TextView的坐标值
				int[] heaDlocal = new int[2];
				curTxt.getLocationOnScreen(heaDlocal);
				// x轴方向偏差值
				offset = heaDlocal[0];
				Log.i(TAG, "偏差值offset:" + offset);
				hasOffset = true;
			}
			break;
		}
		return super.dispatchTouchEvent(ev);
	}

	/**
	 * 设置小方框的移动动画效果(计算了偏差,细节效果更好), 此处的偏差需要演示的时候才能观察到
	 * 
	 * @param endTxt
	 */
	private void imgTransMod(TextView endTxt) {
		// 当前TextView的中心点
		int startMid = curTxt.getLeft() + curTxt.getWidth() / 2;
		// 移动开始位置左边缘
		int startLeft = startMid - imgTransBg.getWidth() / 2;
		// 目的TextView的中心点
		int endMid = endTxt.getLeft() + endTxt.getWidth() / 2;
		// 移动结束位置左边缘
		int endLeft = endMid - imgTransBg.getWidth() / 2;
		// 构造动画
		TranslateAnimation move = new TranslateAnimation(startLeft, endLeft, 0,
				0);
		move.setDuration(100);
		move.setFillAfter(true);
		imgTransBg.startAnimation(move);

		/*
		 * 以下步骤用于处理ScrollView根据滑块的位置来调整自身的滚动,以便达到更好的视觉效果
		 */
		int[] location = new int[2];
		// 获取目的TextViw在当前屏幕中的坐标点,主要用到X轴方向坐标:location[0]
		endTxt.getLocationOnScreen(location);

		// 调整ScrollView的位置
		if ((location[0] - offset) < 0) {
			Log.i(TAG, "超出左屏幕");
			// 目的位置超出左边屏幕,则调整到紧靠该位置的左边
			// 此处ScrollView直接根据位置点滑动
			horiScroll.smoothScrollTo(endTxt.getLeft(), 0);
		} else if ((location[0] + endTxt.getWidth()) > disWidth) {
			Log.i(TAG, "超出右屏幕");
			Log.i(TAG, "移动像素点:"
					+ (location[0] + offset + endTxt.getWidth() - disWidth));
			// 目的位置超出右边屏幕,则调整到紧靠该位置的右边
			// 此处ScrollView需计算滑动距离
			horiScroll.smoothScrollBy(location[0] + offset + endTxt.getWidth()
					- disWidth, 0);
		} else {
			// 如果没超出屏幕,也需保持原地滑动
			// 若不加该效果,则滑块可能出现动画延迟或停滞
			horiScroll.smoothScrollTo(horiScroll.getScrollX(), 0);
		}

		// 切换字体颜色
		curTxt.setTextColor(getResources().getColor(R.color.gray));
		endTxt.setTextColor(getResources().getColor(R.color.white));

		// 更新当前TextView的记录
		curTxt = endTxt;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.txv_new_center:
			// 启动滑块移动动画
			imgTransMod(txv_new_center);
			// 更新Page页
			vPager_Sc.setCurrentItem(0);
			break;
		case R.id.txv_new_inland:
			imgTransMod(txv_new_inland);
			vPager_Sc.setCurrentItem(1);
			break;
		case R.id.txv_new_internation :
			imgTransMod(txv_new_internation );
			vPager_Sc.setCurrentItem(2);
			break;
		case R.id.txv_new_society :
			imgTransMod(txv_new_society );
			vPager_Sc.setCurrentItem(3);
			break;
		case R.id.txv_new_war :
			imgTransMod(txv_new_war );
			vPager_Sc.setCurrentItem(4);
			break;
		case R.id.txv_new_sport :
			imgTransMod(txv_new_sport );
			vPager_Sc.setCurrentItem(5);
			break;
		case R.id.txv_new_it :
			imgTransMod(txv_new_it );
			vPager_Sc.setCurrentItem(6);
			break;
		case R.id.txv_new_digital :
			imgTransMod(txv_new_digital );
			vPager_Sc.setCurrentItem(7);
			break;
		case R.id.txv_new_phone :
			imgTransMod(txv_new_phone );
			vPager_Sc.setCurrentItem(8);
			break;
		case R.id.txv_new_computer :
			imgTransMod(txv_new_computer );
			vPager_Sc.setCurrentItem(9);
			break;
		case R.id.txv_new_fun  :
			imgTransMod(txv_new_fun  );
			vPager_Sc.setCurrentItem(10);
			break;
		case R.id.txv_new_hot  :
			imgTransMod(txv_new_hot  );
			vPager_Sc.setCurrentItem(11);
			break;
		case R.id.txv_new_travel  :
			imgTransMod(txv_new_travel  );
			vPager_Sc.setCurrentItem(12);
			break;
		case R.id.txv_new_star  :
			imgTransMod(txv_new_star  );
			vPager_Sc.setCurrentItem(13);
			break;
		case R.id.txv_new_china_football  :
			imgTransMod(txv_new_china_football  );
			vPager_Sc.setCurrentItem(14);
			break;
		case R.id.txv_new_national_football  :
			imgTransMod(txv_new_national_football  );
			vPager_Sc.setCurrentItem(15);
			break;
		case R.id.txv_new_nba  :
			imgTransMod(txv_new_nba  );
			vPager_Sc.setCurrentItem(16);
			break;
		case R.id.txv_new_cba   :
			imgTransMod(txv_new_cba   );
			vPager_Sc.setCurrentItem(17);
			break;
		case R.id.img_main_more   :
			Intent intent = new Intent( MainViewPagerActivity.this, SettingsActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.left_out);
			break;
		}
	}

	@Override
	public void onPageSelected(int arg0) {
		System.out.println("onPageSelected:  " +arg0);
		switch (arg0) {
		case 0:
			// 直接关联点击事件
			txv_new_center .performClick();
			break;
		case 1:
			txv_new_inland .performClick();
			break;
		case 2:
			txv_new_internation .performClick();
			break;
		case 3:
			txv_new_society .performClick();
			break;
		case 4:
			txv_new_war .performClick();
			break;
		case 5:
			txv_new_sport .performClick();
			break;
		case 6:
			txv_new_it .performClick();
			break;
		case 7:
			txv_new_digital .performClick();
			break;
		case 8:
			txv_new_phone .performClick();
			break;
		case 9:
			txv_new_computer .performClick();
			break;
		case 10:
			txv_new_fun .performClick();
			break;
		case 11:
			txv_new_hot .performClick();
			break;
		case 12:
			txv_new_travel .performClick();
			break;
		case 13:
			txv_new_star .performClick();
			break;
		case 14:
			txv_new_china_football .performClick();
			break;
		case 15:
			txv_new_national_football .performClick();
			break;
		case 16:
			txv_new_nba .performClick();
			break;
		case 17:
			txv_new_cba .performClick();
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		System.out.println("onPageScrollStateChanged:  " +arg0);
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		System.out.println("onPageScrolled:  " +arg0);
		System.out.println("onPageScrolled:  " +arg1);
		System.out.println("onPageScrolled:  " +arg2);
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if (System.currentTimeMillis() - exitTime > 2000) {
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				this.finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
